package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.ShowCommentAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.ShowDetailsAllPicAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.ShowComment;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.ShowUnitPic;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Slideshow;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ShowModel;

/**
 * Created by Lix on 2018/10/15.
 */

public class ShowDetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView,recyclerView_pic;
    private ImageView user_img,show_img;
    private TextView user_name,show_time,show_intro;
    private Button sumComment;
    private EditText comment;
    private SharedPreferences sp;
    private int uid,sid;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showdetails);
        initView();
        initEvent();
        ShowModel showModel =new ShowModel();
        showModel.getShow(sid,getShowListener);
        showModel.getComment(sid,getCommListener);

    }

    TListener<Slideshow> getShowListener =new TListener<Slideshow>() {
        @Override
        public void onResponse(Slideshow slideshow) {
             ArrayList picList = new ArrayList();
             String allpic = slideshow.getPic();
            user_name.setText(slideshow.getNickname());
            show_time.setText(slideshow.getOpentime());
            show_intro.setText(slideshow.getIntro());
            Picasso.with(getApplicationContext())
                    .load(Server.PIC_URL_J+slideshow.getUser_pic())
                    .placeholder(R.drawable.ic_default_img)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(user_img);


            String[] Str1Array = allpic.split("#");//结果Str1Array：[**.jpg, **.jpg, **.jpg]
            for (int j = 0;j < Str1Array.length;j++){
                if(Str1Array[j]!=""){
                    picList.add(new ShowUnitPic(Str1Array[j]));
                }
            }
            ShowDetailsAllPicAdapter showDetailsAllPicAdapter = new ShowDetailsAllPicAdapter(getApplicationContext(),picList);
            recyclerView_pic.setAdapter(showDetailsAllPicAdapter);
          //  Toast.makeText(ShowDetailsActivity.this,"照片："+Str1Array[0], Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    List_TListener<ShowComment> getCommListener = new List_TListener<ShowComment>() {
        @Override
        public void onResponse(List<ShowComment> list) {
            ShowCommentAdapter showCommentAdapter = new ShowCommentAdapter(getApplicationContext(),list);
            recyclerView.setAdapter(showCommentAdapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initView(){
        recyclerView = (RecyclerView)findViewById(R.id.rv_comment);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView_pic = (RecyclerView)findViewById(R.id.rv_show_allpic);
        GridLayoutManager gridLayoutManager =new GridLayoutManager(this,3);
        recyclerView_pic.setLayoutManager(gridLayoutManager);

        user_img = (ImageView)findViewById(R.id.show_detailds_user_img);

        user_name = (TextView)findViewById(R.id.show_details_user_name);
        show_time = (TextView)findViewById(R.id.show_details_time);
        show_intro = (TextView)findViewById(R.id.show_detaisl_intro);

        sumComment = (Button)findViewById(R.id.sum_comment_btn);
        comment = (EditText)findViewById(R.id.sum_comment);

        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        sid = getIntent().getIntExtra("show_id",1);
        uid = sp.getInt("uid",4);
    }
    private void initEvent(){
        sumComment.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.sum_comment_btn:
                final ShowModel showModel = new ShowModel();
                showModel.subComment(uid, comment.getText().toString(), sid, new TListener<Success>() {
                    @Override
                    public void onResponse(Success success) {
                        if (success.getError().equals("0")){
                            showModel.getComment(sid,getCommListener);
                            comment.setText("");
                        }

                    }

                    @Override
                    public void onFail(String msg) {

                    }
                });
                break;
        }

    }


}
